package cn.ym.mall.controller;

import cn.ym.mall.entity.Complaints;
import cn.ym.mall.service.ComplaintsService;
import cn.ym.mall.service.impl.ComplaintsServiceImpl;
import cn.ym.mall.utils.PrintUtils;
import cn.ym.mall.vo.CommonResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.time.LocalDateTime;

/**
 * 投诉举报
 */
public class ComplaintsController {
    private ComplaintsService complaintsService = new ComplaintsServiceImpl();

    /**
     * 新增投诉举报
     * @param request
     * @param response
     * @throws SQLException
     * @throws IOException
     */
    public void addComplaints(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        Integer sid = Integer.valueOf(request.getParameter("sid"));
        String tscontent = request.getParameter(("tscontent"));
        String tsfeedback = request.getParameter(("tsfeedback"));
        String tsitsstated = request.getParameter(("tsitsstated"));
        Complaints complaints = new Complaints();
        complaints.setUid(uid);
        complaints.setSid(sid);
        complaints.setTscontent(tscontent);
        complaints.setTsfeedback(tsfeedback);
        complaints.setTstime(LocalDateTime.now());
        complaints.setTsitsstated(tsitsstated);
        PrintUtils.print(response, CommonResult.success(complaintsService.addComplaints(complaints)));
    }

    /**
     * 通过投诉编号查询信息
     * @param request
     * @param response
     * @throws SQLException
     * @throws IOException
     */
    public void getComplaintsByTid(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        Integer tsid = Integer.valueOf(request.getParameter("tsid"));
        PrintUtils.print(response,complaintsService.getComplaintsByTid(tsid));
    }

    /**
     * 查询所有投诉信息（分页）
     * @param request
     * @param response
     * @throws SQLException
     * @throws IOException
     */
    public void getPageComplaints(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        Integer pageNo = request.getParameter("pageNo")==null?1:Integer.valueOf(request.getParameter("pageNo"));
        Integer pageSize = request.getParameter("pageSize")==null?10:Integer.valueOf(request.getParameter("pageSize"));
        PrintUtils.print(response,complaintsService.getPageComplaints(pageNo,pageSize));
    }
}
